const Response = require('node-spider').Response;
const Request = require('node-spider').Request;
let Spider = require('node-spider').Spider;
const querystring = require('querystring');

module.exports.Spider = class extends Spider{
    constructor(){
        super();
        this.urls=['https://movie.douban.com/chart'];
    }
    //yield关键字返回数据或者新的请求
    *parse(response){
        console.log("start");
        let $ = response.selector;
        let movies = $('.types').find('a');
        for(let i =0;i<movies.length;i++){
            console.log(movies.eq(i).text()+movies.eq(i).prop('href'));
            if (movies.eq(i).text()=='剧情')
                yield new Request({url:'https://movie.douban.com',callback:this.parse_content}).joinUrl('/j/chart/top_list'+encodeURI(movies.eq(i).prop('href').replace('/typerank','')+'&start=0&limit=20'));
        }
    }
    *parse_content(response){
        let movies = JSON.parse(response.text);
        if (movies.length<=0){
            console.log('全部遍历...退出程序...'+response.url);
            return ;
        }
        console.log('-------------------------------------------------------'+movies.length);
        // console.log(response.url);
        let url  = response.url.split('?');
        let par = querystring.parse(url[1]);
        // console.log(url);
        par.start = Number.parseInt(par.start)+20;
        url = url[0]+'?'+querystring.stringify(par);
        //返回下一页的请求
        yield new Request({url:url,callback:this.parse_content});
        for(let movie of movies){
            yield '片名：'+movie.title+' 地址：'+movie.url+' 评分:'+movie.score;
        }
    }
}
